import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import LayoutView from '../views/LayoutView.vue'
import AdminView from '../views/AdminView.vue'
import UserView from '../views/UserView .vue'
import SuperView from '../views/SuperUserView.vue'
import ProrView from '../views/ProrOut.vue'
import virtualView from '../views/VirtualCharacter.vue'
import regionlView from '../views/RegionTree.vue'
import messageView from '../views/Message.vue'
import registView from '../views/regist.vue'









Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: LoginView,
    meta:{
      title:"校园树洞平台"
    }
  },
  {
    path: '/regist',
    name: 'regist',
    component: registView,
    meta:{
      title:"校园树洞平台"
    }
  },
  {
    path: '/',
    name: 'Layout',
    component: LayoutView,
    children:[
      {
        path: '',
        name: 'home',
        component: HomeView
      },
      {
        path: 'admin',
        name: 'admin',
        component: AdminView
      },
      {
        path: 'user',
        name: 'user',
        component: UserView
      },
      {
        path: 'super',
        name: 'super',
        component: SuperView
      },
      {
        path: 'pror',
        name: 'pror',
        component: ProrView
      },
      {
        path: 'virtual',
        name: 'virtual',
        component: virtualView
      },
      {
        path: 'region',
        name: 'region',
        component: regionlView
      },
      {
        path: 'message',
        name: 'message',
        component: messageView
      }
    ]
  },
  
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to ,from, next) => {
  if (to.path ==='/login' && to.regist) {
    next();
  }
  const user = localStorage.getItem("user");
  
  if (!user && to.path !== '/login' && to.path !== '/regist') {
    return next("/login");
  }
  next();
})

export default router
